package ptij.vocoder.baseModel.common;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.IntToken;
import ptolemy.data.expr.Parameter;
import ptolemy.data.type.*;

import ptij.vocoder.baseModel.*;

import java.util.*;

//////////////////////////////////////////////////////////////////////////
//// AssembleFrame
/**

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class AssembleFrame extends TypedAtomicActor {

    public AssembleFrame(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        input = new TypedIOPort(this, "input", true, false);
        input.setTypeEquals(BaseType.OBJECT);

        output = new TypedIOPort(this, "output", false, true);
        output.setTypeEquals(BaseType.OBJECT);

        assemble = new Parameter(this, "assemble", new IntToken(4));
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports
    public TypedIOPort input;

    // Output Ports
    public TypedIOPort output;

    public Parameter assemble;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();

        IntToken it = (IntToken) assemble.getToken();
        _assemble = it.intValue();
    }

    public void fire() throws IllegalActionException {
        LinkedList ll = new LinkedList();
        int size = 0;
        for(int i = 0;i < _assemble;i++) {
            ObjectToken obj = (ObjectToken) input.get(0);
            Short [] v = (Short []) obj.getValue();
            ll.add(v);
            size += v.length;
        }

        Short [] newframe = new Short[size];
        int pos = 0;
        for(int i = 0;i < _assemble;i++) {
            Short [] s = (Short []) ll.get(i);
            for(int k = 0;k < s.length;k++) {
                newframe[pos] = s[k];
                pos++;
            }
        }

        output.broadcast(new ObjectToken(newframe));
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////


    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private int _assemble;
}

